package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.*;
import csu.web.mypetstore.persistence.AccountDao;
import csu.web.mypetstore.persistence.ItemDao;
import csu.web.mypetstore.persistence.impl.AccountDaoImpl;
import csu.web.mypetstore.persistence.impl.ItemDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class UpdateCartAJAXServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        AccountDao accountDao = new AccountDaoImpl();
        Account loginAccount = (Account) session.getAttribute("loginAccount");

        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();

        Cart cart = (Cart) session.getAttribute("cart");
        if(cart==null){
            cart = new Cart();
        }

        Iterator<CartItem> cartItemss= cart.getAllCartItems();
        List<CartItem> cartItems = cart.getCartItemList();

        while (cartItemss.hasNext()) {
            CartItem cartItem = (CartItem) cartItemss.next();
            String itemId = cartItem.getItem().getItemId();
            try {
                String quantityString = req.getParameter(itemId);
                int quantity =Integer.parseInt(quantityString);

                cart.setQuantityByItemId(itemId, quantity);
                if (quantity < 1) {
                    cartItemss.remove();
                }
            } catch (Exception e) {
                //ignore parse exceptions on purpose
            }
        }

        String quantity = req.getParameter("quantity");
        String itemId = req.getParameter("itemId");

        if (loginAccount != null) {

            if(accountDao.selectShoppingcartByUsernameAndTimes(loginAccount)){
                //数据库中有购物信息，提取信息
                List<CartItem> cartItems2= new ArrayList<>();
                List<Item> itemList= new ArrayList<>();
                List<Product> productList= new ArrayList<>();

                accountDao.selectShoppingcart(loginAccount,cartItems2,itemList,productList);
                //提取数据库信息

                for(int i=0;i<itemList.size();i++){
                    cart.addItem(itemList.get(i),cartItems2.get(i).isInStock());
                    cart.setQuantityByItemId(itemList.get(i).getItemId(),cartItems2.get(i).getQuantity());
                }

                for(int i=0;i<cartItems.size();i++){
                    CartItem cartItem = cartItems.get(i);
                    Item item = cartItems.get(i).getItem();
                    String itemId1 = item.getItemId();
                    if(itemId1.equals(itemId)){
                        cart.setQuantityByItemId(itemList.get(i).getItemId(), Integer.parseInt(quantity));
                        accountDao.updateShoppingcartByUsernameAndItemid(loginAccount,cartItem);
                        break;
                    }
                }

                session.setAttribute("cart",cart);
                session.setAttribute("cartItems",cartItems2);
            }

        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
